package com.sc.test;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.bean.News;
import com.sc.test.bean.URLS;
import com.sc.test.utils.AppUrls;
import com.sc.test.utils.Util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Shen Chao on 2017-12-27-027.
 */

public class RetrofitActivity extends AppCompatActivity {
    AppUrls appUrls, appUrls1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
        ButterKnife.bind(this);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URLS.BASE_URL)
                .build();
        appUrls = retrofit.create(AppUrls.class);
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl(URLS.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        appUrls1 = retrofit1.create(AppUrls.class);
    }

    @OnClick({R.id.btn_retrofit_get, R.id.btn_retrofit_post, R.id.btn_retrofit_getAndQuery
            , R.id.btn_retrofit_getReturnBean, R.id.btn_retrofit_postForm, R.id.btn_retrofit_postMap})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_retrofit_get:
                Call<ResponseBody> call = appUrls.getNewsUrl("index");
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String result = response.body().string();
                            Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
                break;
            case R.id.btn_retrofit_getAndQuery:
                Call<ResponseBody> call1 = appUrls.getNewsUrlAndKey(URLS.JUHE_APPKEY);
                call1.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String result = response.body().string();
                            Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
                break;
            case R.id.btn_retrofit_getReturnBean:
                Call<News> newsCall = appUrls1.getNewsUrlAndKeyReturnBean(URLS.JUHE_APPKEY);
                newsCall.enqueue(new Callback<News>() {
                    @Override
                    public void onResponse(Call<News> call, Response<News> response) {
                        String result = response.body().toString();
                        Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                    }

                    @Override
                    public void onFailure(Call<News> call, Throwable t) {

                    }
                });
                break;
            case R.id.btn_retrofit_post:
                Call<News> newsCall1 = appUrls1.getNewsPost(URLS.JUHE_APPKEY);
                newsCall1.enqueue(new Callback<News>() {
                    @Override
                    public void onResponse(Call<News> call, Response<News> response) {
                        News news = response.body();
                        String result = news.getResult().getStat();
                        Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                    }

                    @Override
                    public void onFailure(Call<News> call, Throwable t) {

                    }
                });
                break;
            case R.id.btn_retrofit_postForm:
                Call<News> newsCall2 = appUrls1.getNewsPostForm(URLS.JUHE_APPKEY, "yule");
                newsCall2.enqueue(new Callback<News>() {
                    @Override
                    public void onResponse(Call<News> call, Response<News> response) {
                        News news = response.body();
                        List<News.ResultBean.DataBean> data = news.getResult().getData();
                        String result = data.get(0).getTitle();
                        Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                    }

                    @Override
                    public void onFailure(Call<News> call, Throwable t) {

                    }
                });
                break;
            case R.id.btn_retrofit_postMap:
                Map<String, String> map = new HashMap<>();
                map.put("type", "keji");
                map.put("key", URLS.JUHE_APPKEY);
                Call<News> newsCall3 = appUrls1.getNewsPostMap(map);
                newsCall3.enqueue(new Callback<News>() {
                    @Override
                    public void onResponse(Call<News> call, Response<News> response) {
                        News news = response.body();
                        List<News.ResultBean.DataBean> data = news.getResult().getData();
                        String result = data.get(0).getDate() + data.get(0).getTitle();
                        Util.openNewActivity(RetrofitActivity.this, ResultActivity.class, result);
                    }

                    @Override
                    public void onFailure(Call<News> call, Throwable t) {

                    }
                });
                break;
        }
    }
}
